package org.joshsacks.fishies.ui.gl;

import javax.microedition.khronos.opengles.GL10;

import org.joshsacks.fishies.FishiesController;
import org.joshsacks.fishies.GameBoard.MatchSet;
import org.joshsacks.fishies.GameBoard.MatchSet.Match;

import android.content.Context;

/**
 * Renders the a set of matches, blinking the transparency on the items.
 */
public class MatchSetRenderer {
  private static final long ANIMATION_CYCLE_TIME = 500;
  private static final float TEXT_ANIMATION_TIME = 1400;
  private final MatchSet matchSet;
  private final TextureManager textureManager;
  private final long startTime;
  private final FishiesController controller;
  private RenderedText[] scoreText;
  private GL10 discardGL;
  
  public MatchSetRenderer(FishiesController controller, 
          MatchSet matchSet, TextureManager textureManager, FontManager fontManager) {
    this.controller = controller;
    this.matchSet = matchSet;
    this.textureManager = textureManager;
    this.startTime = System.currentTimeMillis();
  }

  public void render(Context context, GL10 gl) {
    if (discardGL == null) {
      discardGL = gl;
      scoreText = new RenderedText[matchSet.getMatches().size()];
      for (int i = 0; i < scoreText.length; i++) {
        int score = controller.getScore(matchSet.getMatches().get(i));
        scoreText[i] = new BitmapRenderedText(gl, "" + score, 32);
      }
    }
    
    double delta = System.currentTimeMillis() - startTime;
    float alpha = (float) ((1 + Math.cos(delta / ANIMATION_CYCLE_TIME * Math.PI)) / 2);
    for (int m = 0; m < matchSet.getMatches().size(); m++) {
      Match match = matchSet.getMatches().get(m);
      textureManager.setTexture(gl, match.getType().ordinal());
      gl.glColor4f(0, 0, 0, alpha);
      SquareRenderer renderer = new SquareRenderer(gl);
      for (int i = 0; i < match.getLength(); i++) {
        int x = match.getXCoor(i);
        int y = match.getYCoor(i);
        renderer.addSquare(
                x * GameBoardRenderer.getTileSize(), y * GameBoardRenderer.getTileSize(),
                GameBoardRenderer.getTileSize(), false);      
      }     
      renderer.draw();
    }
    
    for (int m = 0; m < matchSet.getMatches().size(); m++) {
      Match match = matchSet.getMatches().get(m);
      int xTotal = 0;
      int yTotal = 0;
      for (int i = 0; i < match.getLength(); i++) {
        xTotal += match.getXCoor(i);;
        yTotal +=  match.getYCoor(i);;
      }     
      xTotal /= match.getLength();
      yTotal /= match.getLength();

      float scoreAlpha = (float) delta / TEXT_ANIMATION_TIME;
      gl.glColor4f(1.0f, 1.0f - scoreAlpha / 2, 1.0f - scoreAlpha, 1.0f - scoreAlpha / 2);
      float xDrift = xTotal < 4 ? scoreAlpha * 70 : -scoreAlpha * 70;
      scoreText[m].draw(gl, 
              xTotal * GameBoardRenderer.getTileSize() + xDrift, 
              yTotal * GameBoardRenderer.getTileSize() + scoreAlpha * 130);
    }
  } 

  public void discard() {
    if (discardGL != null) {
      for (int i = 0; i < scoreText.length; i++) {
        scoreText[i].discard(discardGL);
      }  
    }
  }
}
